package com.kidback.bbloc259.ui.account;

import android.graphics.Color;
import android.view.Gravity;
import android.view.View;

import com.kidback.bbloc259.api.BBNetApi;
import com.kidback.bbloc259.app.KidApplication;
import com.kidback.bbloc259.base.BaseActivity;
import com.kidback.bbloc259.base.lang.Delay;
import com.kidback.bbloc259.base.lang.Runnable0;
import com.kidback.bbloc259.databinding.RegisterActivityBinding;
import com.kidback.bbloc259.network.base.ApiHelper;
import com.kidback.bbloc259.network.beans.BaseResponse;
import com.kidback.bbloc259.network.errorhandler.ResponseThrowable;
import com.kidback.bbloc259.network.observer.BaseObserver;
import com.kidback.bbloc259.ui.WebViewActivity;
import com.kidback.bbloc259.util.LogUtils;
import com.kidback.bbloc259.util.StatusBarUtils;
import com.kidback.bbloc259.util.StringUtils;
import com.kidback.bbloc259.util.TipUtils;
import com.kidback.bbloc259.util.ToastUtils;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class RegisterActivity extends BaseActivity<RegisterActivityBinding> {


    @Override
    public RegisterActivityBinding getViewBinding() {
        return RegisterActivityBinding.inflate(inflater);
    }

    @Override
    protected void initView() {

        StatusBarUtils.setStatusBarColor(activity, 0xff715af3);

        viewBinding.titleBar.setTintColor(Color.WHITE);

        viewBinding.titleBar.setOnBackClickListener(onBackClickListener);


        viewBinding.btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });

        viewBinding.tvLinkPrivate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WebViewActivity.start(v.getContext(), "隐私政策", "https://www.kidback.top/BBLocationWeb/%E9%9A%90%E7%A7%81%E6%94%BF%E7%AD%96.html");
            }
        });

        viewBinding.tvLinkUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WebViewActivity.start(v.getContext(), "用户协议", "https://www.kidback.top/BBLocationWeb/%E7%94%A8%E6%88%B7%E5%8D%8F%E8%AE%AE.html");
            }
        });

    }

    @Override
    protected void loadData() {

    }

    private void register() {
        if (!viewBinding.chkRead.isChecked()) {
            TipUtils.showMsgDuration(KidApplication.getCurrentActivity(), "请先阅读隐私协议和用户协议", Gravity.CENTER, 1000);
            return;
        }

        String phone = viewBinding.edtPhone.getText().toString();
        String password = viewBinding.edtPwd.getText().toString();
        String passwordRepeat = viewBinding.edtPwdRepeat.getText().toString();

        if (StringUtils.length(phone) < 6 ) {
            ToastUtils.show("账号至少需要6位字条");
            return;
        }
        if (StringUtils.length(password) != 8) {
            ToastUtils.show("密码长度必须为8位");
            return;
        }
        if (!StringUtils.equals(password, passwordRepeat)) {
            ToastUtils.show("两次输入的密码不相同");
            return;
        }

        Observable<BaseResponse<Void>> observable = BBNetApi.getBBService().register(phone, password);
        Observer<BaseResponse<Void>> observer = new BaseObserver<BaseResponse<Void>>() {

            @Override
            public void onSubscribe(Disposable d) {
                super.onSubscribe(d);
            }

            @Override
            public void onSuccess(BaseResponse<Void> orderResponse) {
                TipUtils.hideLoadingFixDialog();

                TipUtils.showMsgFixed(KidApplication.getCurrentActivity(), "注册成功", Gravity.CENTER);

                Delay.delay(1000, new Runnable0() {
                    @Override
                    public void run() {
                        finish();
                    }
                });
            }

            @Override
            public void onFailure(ResponseThrowable e) {
                TipUtils.hideLoadingFixDialog();

                TipUtils.showMsgFixed(KidApplication.getCurrentActivity(), "注册失败\n" + e.getMessage());
            }
        };

        ApiHelper.request(observable, observer);

        TipUtils.showLoadingFixDialog(KidApplication.getCurrentActivity(), "正在注册");
    }

}
